﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Specifications;
using XfTech.SmartTms.Entities.Issues;
using XfTech.SmartTms.EntityFrameworkCore;

namespace XfTech.SmartTms.Repositories
{
    public class IssueRepository :
       EfCoreRepository<SmartTmsDbContext, Issue, Guid>,
       IIssueRepository
    {
        public IssueRepository(IDbContextProvider<SmartTmsDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 创建问题
        /// </summary>
        /// <param name="issue"></param>
        /// <param name="labels"></param>
        /// <returns></returns>
        public async Task<Issue> CreateIssuesAsync(Issue issue)
        {
            var dbContext = await GetDbContextAsync();

            //添加到数据库
            Issue savedEntity = (await dbContext.Set<Issue>().AddAsync(issue)).Entity;

            //两个表的修改一起提交
            await dbContext.SaveChangesAsync();

            return savedEntity;
        }

        /// <summary>
        /// 获取问题列表
        /// </summary>
        /// <param name="spec"></param>
        /// <returns></returns>
        public async Task<List<Issue>> GetIssuesAsync(ISpecification<Issue> spec)
        {
            var dbContext = await GetDbContextAsync();

            return await dbContext.Set<Issue>()
                .Where(spec.ToExpression())
                .ToListAsync();
        }
    }
}
